<?php
declare (strict_types = 1);
namespace app\api\logic;
use app\common\model\Cart as cartModel;
use app\admin\model\Goods as pro;
use app\admin\model\GoodsSpecs as spe;
class CartLogic
{
	function add($args = []){
			 $num = $args['num'] ?? 1;
	    	 $pid = $args['pid'] ?? 0;
	    	 $specs_id =  $args['specs_id'] ?? 0;
	    	 if($pid){
	    	 	  $pro = pro::where(['id'=>$pid,'shop_id'=>$args['shop_id']])->where("status",1)->find();
	    	 	  if(!$pro){
	    	 	  		return ['code'=>100,'back_type'=>1,'msg'=>'商品不存在或已下架'];
	    	 	  }
	    	 	  if($num > $pro['stock']){
	    	 	  	    return ['code'=>100,'back_type'=>1,'msg'=>'当前购买量超过库存'];
	    	 	  }
	    	 	  if(intval($pro['limit_num']) > 0){ // 如果限购了
	    	 	  		if($num > $pro['limit_num']){
	    	 	  			 return ['code'=>100,'back_type'=>1,'msg'=>'该商品限购'.$pro['limit_num'].'件'];
	    	 	  		}
	    	 	  		$yimai_num = cartModel::where(['user_id'=>$args['uid'],'is_pay'=>1,'pro_id'=>$pid])->sum("pro_num");
	    	 	  		$cart_pro_num = cartModel::where(['user_id'=>$args['uid'],'is_see'=>0,'pro_id'=>$pid])->sum("pro_num");
	    	 	  		if($yimai_num >= $pro['limit_num']){
	    	 	  			 return ['code'=>100,'back_type'=>1,'msg'=>'该商品为限购商品，您已下单过，去看看其他商品吧~'];	
	    	 	  		}else{
	    	 	  			 $kemai = intval($pro['limit_num']) - intval($yimai_num);

	    	 	  			 if(($num+$cart_pro_num) > $kemai){
	    	 	  			 	  return ['code'=>100,'back_type'=>1,'msg'=>'该商品为限购商品，您当前仅可购买'.$kemai.'件'];	
	    	 	  			 }	
	    	 	  		}
	    	 	  }
	    	 	  $can = '默认';
	    	 	  $price = $pro['price'];
	    	 	  if($specs_id){
	    	 	  	  $specs = spe::where("id",$specs_id)->find();
	    	 	  	  $can = $specs['name'];
	    	 	  	  $price = $specs['price'];
	    	 	  }

	    	 	  $findcart = cartModel::where(['user_id'=>$args['uid'],'is_see'=>0,'can'=>$can,'pro_id'=>$pid])->find();
 
	    	 	  if($findcart){
	    	 	  	  $res = cartModel::where(['user_id'=>$args['uid'],'is_see'=>0,'can'=>$can,'pro_id'=>$pid])->setInc("pro_num",intval($num));
	    	 	  }else{
	    	 	  	  $res = cartModel::insert([
	    	 	  	   		'pro_num'=>$num,
	    	 	  	   		'user_id'=>$args['uid'],
	    	 	  	   		'can'=>$can,
	    	 	  	   		'pro_title'=>$pro['title'],
	    	 	  	   		'pro_img'=>$pro['feng'],
	    	 	  	   		'pro_price'=>$price,
	    	 	  	   		'pro_cid'=>$pro['cat_id'],
	    	 	  	   		'pro_id' =>$pro['id'],
	    	 	  	   		'shop_id'=>$args['shop_id']

	    	 	  	   ]);
	    	 	  }

		 	     if($res){
		    	 	 return ['code'=>200,'back_type'=>1,'msg'=>'ok'];
		    	 }else{
		    	 	 return ['code'=>100,'back_type'=>1,'msg'=>'操作失败'];
		    	 }		  	
		  }else{
		  	   return ['code'=>100,'back_type'=>1,'msg'=>'请选择商品'];
		  }
	 
   }

   function del($args = []){
    	 $cid = intval($args['cid'] ?? 0);
    	 if($cid == 0){
    	 	 return ['code'=>100,'back_type'=>1,'msg'=>'参数缺失'];
    	 }
    	 $w['is_see'] = 0;
    	 $w['user_id'] = $args['uid'];
    	 $w['cid'] = array("in",$cid);
    	 $w['shop_id'] = $args['shop_id'];
    	 $res = cartModel::where($w)->delete();
    	 if($res){
    	 	   return ['code'=>200,'back_type'=>1,'msg'=>'ok'];
    	 }else{
    	 	   return ['code'=>100,'back_type'=>1,'msg'=>'删除失败'];
    	 }
   }

   function lists($args = []){
   		 
    	 $uid = $args['uid'];
    	 $w['is_see'] = 0;
    	 $w['user_id'] = $uid;
    	 $w['shop_id'] = $args['shop_id'];
    	 $res = cartModel::where($w)->select();
   		 return $res;
   }

   function handleNum($args = []){
   		 $type = $args['type'] ?? 1;
   		 $cid = $args['cid'] ?? 0;
   		 if($cid > 0){
   		 	  if($type == 1){
   		 	  	 $s = cartModel::where(['user_id'=>$args['uid'],'is_see'=>0,'cid'=>$cid])->setInc("pro_num");
   		 	  }else{
   		 	  	 $s = cartModel::where(['user_id'=>$args['uid'],'is_see'=>0,'cid'=>$cid])->setDec("pro_num");
   		 	  }
   		 	  if($s){
   		 	  	  return ['code'=>200,'back_type'=>1,'msg'=>'ok']; 
   		 	  }else{
   		 	  	  return ['code'=>100,'back_type'=>1,'msg'=>'操作失败，请重试'];
   		 	  }
   		 }else{
   		 	  return ['code'=>100,'back_type'=>1,'msg'=>'错误操作'];
   		 }
   }

   function currentNum($args = []){
   		if($args['uid']){
   			  $num = cartModel::where(['is_see'=>0,'user_id'=>$args['uid']])->count();
   		}
   		return $num ?? 0;
   }

}